﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using DAO;

namespace QLDaiLy
{
   
    public partial class TraCuuCoBan : XtraUserControl
    {
        private int MaChucVu;
        DataTable temp;
        public TraCuuCoBan()
        {
            InitializeComponent();  
        }
        public TraCuuCoBan(int MaChucVu)
        {
            // TODO: Complete member initialization
            InitializeComponent();
            this.MaChucVu = MaChucVu;
            if (MaChucVu == 1)
            {
                gridViewDanhSachDaiLy.Columns["ThuTien"].Visible = false;
                gridViewDanhSachDaiLy.Columns["PhieuXuat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaThat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["CapNhat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaGia"].Visible = false;
            }
            if (MaChucVu == 2)
            {
                gridViewDanhSachDaiLy.Columns["ThuTien"].Visible = false;
                gridViewDanhSachDaiLy.Columns["PhieuXuat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["CapNhat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaGia"].Visible = false;
            }
            if (MaChucVu == 3)
            {
                gridViewDanhSachDaiLy.Columns["ThuTien"].Visible = false;
                gridViewDanhSachDaiLy.Columns["PhieuXuat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaThat"].Visible = false;
            }
            if (MaChucVu == 4)
            {

                gridViewDanhSachDaiLy.Columns["ThuTien"].Visible = false;
                gridViewDanhSachDaiLy.Columns["CapNhat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaGia"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaThat"].Visible = false;
            }
            if (MaChucVu == 5)
            {
                gridViewDanhSachDaiLy.Columns["PhieuXuat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["CapNhat"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaGia"].Visible = false;
                gridViewDanhSachDaiLy.Columns["XoaThat"].Visible = false;
            }
        }
        private void radioGroupSimple_SelectedIndexChanged(object sender, EventArgs e)
        {
            TableLayoutPanel tb = (TableLayoutPanel)this.Parent;
            tb.Controls.Remove(tb.GetControlFromPosition(0, 1));
            TimKiemNangCao frm = new TimKiemNangCao(MaChucVu);
            tb.Controls.Add(frm, 0, 1);
        }
        private void TimKiem()
        {
            var tx = tboxTimKiem.MaskBox;
            AutoCompleteStringCollection customSource = new AutoCompleteStringCollection();
            List<DaiLyDTO> list = new List<DaiLyDTO>();
            list = DaiLyDAO.LoadDanhSachTenDaiLy();
            string[] arr = new string[list.Count];
            int i = 0;
            foreach (DaiLyDTO p in list)
            {
                arr[i] = (p.TenDaiLy);
                i++;
            }
            customSource.AddRange(arr);
            string tendaily = tboxTimKiem.Text;
            DataTable tb = DaiLyDAO.TimKiemDaiLyCoBan(tendaily);
            tx.AutoCompleteSource = AutoCompleteSource.CustomSource;
            tx.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            tx.AutoCompleteCustomSource = customSource;
            temp = tb;
            gridDaiLyControl.DataSource = temp;
        }
        private void btnTimkiem_Click(object sender, EventArgs e)
        {
            TimKiem();
        }
        private void gridViewDanhSachDaiLy_CustomUnboundColumnData(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDataEventArgs e)
        {
            if (e.IsGetData)
            {
                e.Value = QLDaiLy.Properties.Resources.ThuTien;
            }

            if (e.Column.Caption == "Hoá Đơn")
            {
                e.Value = QLDaiLy.Properties.Resources.HoaDon;
            }
            if (e.Column.Caption == "Cập Nhật")
            {
                e.Value = QLDaiLy.Properties.Resources.CapNhatIcon;
            }
            if (e.Column.Caption == "Xoá Giả")
            {
                e.Value = QLDaiLy.Properties.Resources.deletexoagia;
            }
            if (e.Column.Caption == "Xoá Thật")
            {
                e.Value = QLDaiLy.Properties.Resources.delete;
            }
        }
        private void repositoryPhieuThu_Click(object sender, EventArgs e)
        {
            string MaDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["MaDaiLy"]).ToString();
            string TenDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["TenDaiLy"]).ToString();
            string NoCuaDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["NoCuaDaiLy"]).ToString();

            TableLayoutPanel tb = (TableLayoutPanel)this.Parent;
            tb.Controls.Remove(tb.GetControlFromPosition(0, 1));
            PhieuThuTien frm = new PhieuThuTien(MaDaiLy, TenDaiLy, NoCuaDaiLy);
            tb.Controls.Add(frm, 0, 1);
        }
        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        private void repositoryHoaDon_Click(object sender, EventArgs e)
        {
            PhieuXuatDTO px = new PhieuXuatDTO();
            px.MaDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["MaDaiLy"]).ToString();
           // px.TenDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["TenDaiLy"]).ToString();
            TableLayoutPanel tb = (TableLayoutPanel)this.Parent;
            tb.Controls.Remove(tb.GetControlFromPosition(0, 1));
            PhieuXuat frm = new PhieuXuat(px);
            tb.Controls.Add(frm, 0, 1);
        }
        private void repositoryItemCapNhat_Click(object sender, EventArgs e)
        {

            string MaDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["MaDaiLy"]).ToString();
            string TenDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["TenDaiLy"]).ToString();
            string TenLoaiDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["TenLoaiDaiLy"]).ToString();
            string TenQuan = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["TenQuan"]).ToString();
            string DiaChi = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["DiaChi"]).ToString();
            string DienThoai = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["DienThoai"]).ToString();
            string Email = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["Email"]).ToString();
            string NgayTiepNhan = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["NgayTiepNhan"]).ToString();
            TableLayoutPanel tb = (TableLayoutPanel)this.Parent;
            tb.Controls.Remove(tb.GetControlFromPosition(0, 1));
            // CapNhatDaiLy frm = new CapNhatDaiLy(_dl);
            CapNhatDaiLy frm = new CapNhatDaiLy(MaDaiLy, TenDaiLy, TenLoaiDaiLy, TenQuan, DiaChi, DienThoai, Email, NgayTiepNhan);
            tb.Controls.Add(frm, 0, 1);
        }
        private void repositoryItemXoaGia_Click(object sender, EventArgs e)
        {
             DialogResult dr = MessageBox.Show("Bạn có muốn xóa đại lý này không?", "Thoát chương trình", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
             if (dr == DialogResult.OK)
             {
                 string maDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["MaDaiLy"]).ToString();
                 bool kq = DaiLyDAO.XoaGia(maDaiLy);
                 if (kq == true)
                 {
                     MessageBox.Show("Xoá thành công !");
                     // dataGridView.Rows.Add();
                     TimKiem();
                 }
                 else
                 {
                     MessageBox.Show("Có lổi khi xoá đại lý");
                 }
             }
        }

        private void repositoryItemXoaThat_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có muốn xóa đại lý này không?", "Thoát chương trình", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.OK)
            {
                string MaDaiLy = gridViewDanhSachDaiLy.GetRowCellValue(gridViewDanhSachDaiLy.FocusedRowHandle, gridViewDanhSachDaiLy.Columns["MaDaiLy"]).ToString();
                bool kq = DaiLyDAO.deleteDaiLy(MaDaiLy);
                if (kq)
                {
                    // MessageBox.Show("Xoá thành công");
                    TimKiem();
                }
                else
                {
                    MessageBox.Show("Xoá thất bại");
                }
            }
        }
    }
}
